import {
  Body,
  Controller,
  Delete,
  Get,
  Param,
  Post,
  Put,
  Query,
} from '@nestjs/common';
import { RoleService } from './role.service';
import { CreateRoleDto } from './dto/create-role.dto';
import { UpdateRoleDto } from './dto/update-role.dto';
import { RequireLogin, RequirePermission } from 'src/custom.decorator';
import {
  ApiTags,
  ApiOperation,
  ApiResponse,
  ApiBearerAuth,
  ApiQuery,
  ApiParam,
  ApiBody,
} from '@nestjs/swagger';

@ApiTags('角色管理')
@ApiBearerAuth()
@Controller('role')
@RequireLogin()
export class RoleController {
  constructor(private readonly roleService: RoleService) {}

  @Post()
  @RequirePermission('role:create')
  @ApiOperation({ summary: '创建角色' })
  @ApiResponse({ status: 201, description: '创建成功' })
  @ApiResponse({ status: 400, description: '角色已存在' })
  async create(@Body() createRoleDto: CreateRoleDto) {
    return await this.roleService.create(createRoleDto);
  }

  @Get('list')
  @RequirePermission('role:query')
  @ApiOperation({ summary: '获取角色列表' })
  @ApiQuery({
    name: 'pageNum',
    description: '页码',
    required: false,
    type: Number,
  })
  @ApiQuery({
    name: 'pageSize',
    description: '每页数量',
    required: false,
    type: Number,
  })
  @ApiQuery({ name: 'name', description: '角色名称', required: false })
  async findAll(
    @Query('pageNum') pageNum = 1,
    @Query('pageSize') pageSize = 10,
    @Query('name') name?: string,
  ) {
    return await this.roleService.findAll(pageNum, pageSize, name);
  }

  @Get(':id')
  @RequirePermission('role:query')
  @ApiOperation({ summary: '获取角色详情' })
  @ApiParam({ name: 'id', description: '角色ID' })
  async findOne(@Param('id') id: string) {
    return await this.roleService.findOne(+id);
  }

  @Put(':id')
  @RequirePermission('role:update')
  @ApiOperation({ summary: '更新角色' })
  @ApiParam({ name: 'id', description: '角色ID' })
  @ApiResponse({ status: 200, description: '更新成功' })
  @ApiResponse({ status: 400, description: '角色不存在/角色名已存在' })
  async update(@Param('id') id: string, @Body() updateRoleDto: UpdateRoleDto) {
    return await this.roleService.update(+id, updateRoleDto);
  }

  @Delete(':id')
  @RequirePermission('role:delete')
  @ApiOperation({ summary: '删除角色' })
  @ApiParam({ name: 'id', description: '角色ID' })
  @ApiResponse({ status: 200, description: '删除成功' })
  @ApiResponse({
    status: 400,
    description: '角色不存在/该角色下存在用户，无法删除',
  })
  async remove(@Param('id') id: string) {
    return await this.roleService.remove(+id);
  }

  @Post(':id/permissions')
  @RequirePermission('role:permission:update')
  @ApiOperation({ summary: '更新角色权限' })
  @ApiParam({ name: 'id', description: '角色ID' })
  @ApiBody({
    schema: {
      type: 'object',
      properties: {
        permissionIds: {
          type: 'array',
          items: { type: 'number' },
          description: '权限ID数组',
        },
      },
    },
  })
  @ApiResponse({ status: 200, description: '权限更新成功' })
  @ApiResponse({ status: 400, description: '角色不存在' })
  async updatePermissions(
    @Param('id') id: string,
    @Body('permissionIds') permissionIds: number[],
  ) {
    return await this.roleService.updatePermissions(+id, permissionIds);
  }
}
